// 基类
import type{ RequestParams } from "./interface"
const BASEURL = 'https://gmall-prod.atguigu.cn/skb';
export class service{
  private api(params:RequestParams):any{
    uni.showLoading({
      title: '加载中',
      mask: true
    })
    type HeaderType = {
      'Content-type':string,
      token?:string,
    }
    const header:HeaderType = {
      "Content-type":"application/json"
    }
    const token = uni.getStorageSync('token');
    token && (header.token = token);
    return new Promise((resolve,reject)=>{
      uni.request({
        url:BASEURL +  params.url,
        data:params.data,
        method:params.method,
        header,
        success:(res)=>{
          // console.log(res.data);
          resolve(res.data);
        },
        fail:(error)=>{
          // 修改状态为失败
          reject(error)
        },
        complete:()=>{
          uni.hideLoading()
        }
      })
    })
  }
  get(params:RequestParams){
    params.method = 'GET';
    return this.api(params);
  }
  post(params:RequestParams){
    params.method = 'POST';
    return this.api(params);
  }
  put(params:RequestParams){
    params.method = 'PUT';
    return this.api(params);
  }
  delete(params:RequestParams){
    params.method = 'DELETE';
    return this.api(params);
  }
}